<template>
    <el-main>
      <el-form ref="form" :model="form1" label-width="120px" inline>
        <el-form-item label="金融产品选择">
          <el-select v-model="form1.pricductId" placeholder="请选择"  @change="getCreditLine()">
            <el-option v-for="item in this.productType" :label="item.jrProductName" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="授信额度">
          <el-input v-model="form1.creditLine" disabled><i slot="suffix">%</i></el-input>
        </el-form-item><br>
<!--       年一次性服务率 -->
        <el-form-item label="年一次性服务率" >
          <el-input v-model="form1.serviceFee"><i slot="suffix">%</i></el-input>
        </el-form-item>
<!--      年利率  -->
        <el-form-item label="年利率" >
          <el-input v-model="form1.interestRate"><i slot="suffix">%</i></el-input>
        </el-form-item>
      </el-form>
<!--      llll-->
      <el-button type="warning" @click="dialogFormVisible = true">添加租房合同</el-button>
      <!--弹出框-->
      <el-dialog title="租房合同查询" :visible.sync="dialogFormVisible">
        <el-form :model="form2">
          小区名称
          <el-input v-model="form2.premiseName" autocomplete="off" style="width: 120px"></el-input>
          租客姓名
          <el-input v-model="form2.tenantName" autocomplete="off" style="width: 120px"></el-input>
          <el-button type="warning" @click="getContractList">查询</el-button>
        </el-form>
        <el-table
          :data="tableData"
          :row-class-name="rowClassName"
          border
          style="width: 100%">
          <el-table-column
            prop="tenantName"
            label="租客姓名"
            width="180">
          </el-table-column>
          <el-table-column
            prop="roomName"
            label="房源信息"
            width="180">
          </el-table-column>
          <el-table-column
            prop="signingDate"
            label="签约日期">
          </el-table-column>
          <el-table-column
            label="操作">
            <template slot-scope="scope">
              <el-button type="text" style="color:orange" @click="pitchOn(scope.row)">选中</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
      <el-table
        :data="tableData2"
        border
        style="width: 100%">
        <el-table-column
          prop="cfContractId"
          label="序号"
          width="180">
        </el-table-column>
        <el-table-column
          prop="roomName"
          label="房源信息"
          width="180">
        </el-table-column>
        <el-table-column
          prop="roomCode"
          label="房源编号">
        </el-table-column>
        <el-table-column
          prop="tenantName"
          label="租客姓名">
        </el-table-column>
        <el-table-column
          prop="tenantPhone"
          label="租客手机号">
        </el-table-column>
        <el-table-column
          prop="shouldMonthRent"
          label="应收租约金额">
        </el-table-column>
        <el-table-column
          prop="factoringInterest"
          label="保理利息">
        </el-table-column>
        <el-table-column
          prop="signingDate"
          label="借款期限">
          <template slot-scope="scope">
            <span>{{scope.row.factoringPeriod}}个月</span>
            <span>{{scope.row.borrowedStart}}至{{scope.row.borrowedEnd}}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="signingDate"
          label="操作" align="center">
          <template slot-scope="scope">
            <i class="el-icon-delete" @click="toDelete(scope.row.index)" style="color:red"></i>
          </template>
        </el-table-column>

      </el-table>
      <div>
        <span>应收租约合计</span>
        <el-input v-model="form3.rentMoney" disabled style="width: 150px"><i slot="suffix"></i></el-input>
        <span>保理授信金额合计</span>
        <el-input v-model="form3.factoringCreditMoney" disabled style="width: 150px"><i slot="suffix"></i></el-input><br><br>
        <span>服务费</span>
        <el-input v-model="form3.serviceMoney" disabled style="width: 150px"><i slot="suffix"></i></el-input>
        <span>保理利息合计</span>
        <el-input v-model="form3.factoringAdditionalMoney" disabled style="width: 150px"><i slot="suffix"></i></el-input><br><br>
        <span>实收保理金额</span>
        <el-input v-model="form3.factoringTrueMoney" disabled style="width: 150px"><i slot="suffix"></i></el-input>
      </div>
      <div>
        <span>备注</span>
        <el-input type="textarea" style="width:865px" v-model="form3.remark"></el-input><br>
        <el-button type="warning" @click="goSaveJrFactoring">提交申请</el-button>
      </div>
    </el-main>
</template>

<script>
import { listFactoring, getFactoring, delFactoring, addFactoring, updateFactoring,productTypeList,contractList,factoringApply,saveJrFactoring} from "@/api/financial/factoring";

export default {
  data() {
    return {
      form1: {
        serviceFee:'',
        interestRate:'',
        creditLine:'',
        pricductId:'',
      },
      productType:[],
      dialogTableVisible: false,
      dialogFormVisible: false,
      formLabelWidth: '80px',
      form2: {
        premiseName: '',
        tenantName: '',
      },
      form3:{
        serviceFee:'',//一次性服务费
        rate:'',//利率 年利率
        jrProductId:'', //金融产品id
        monthLimit:'', //借款期限 单位月
        jrFactoringDetailList:[],//保理明细
        rentMoney:null, //应收租约合计
        factoringCreditMoney:null, //保理授信金额合计
        serviceMoney:null, //服务费
        factoringAdditionalMoney:null, //保理利息合计
        factoringTrueMoney:null, //实收保理金额
        remark:'',//备注
        contractId:'',
      },
      tableData: [],
      tableData2: [],
    }
  },
  methods: {
    //金融产品下拉框
    getProductTypeList() {
      productTypeList().then(res => {
        console.log(res);
        this.productType = res.data;
      })
    },
    //授信额度回显
    getCreditLine() {
      this.productType.map(res => {
        if (res.id == this.form1.pricductId) {
          this.form1.creditLine = res.creditLine;
        }
      })
    },
    //合同查询 根据小区名租客名
    getContractList() {
      contractList(this.form2).then(res => {
        this.tableData = res.data;
      })
    },
    //获取表一行的数据
    rowClassName({row, rowIndex}) {
      //把每一行的索引放进row
      row.index = rowIndex;
    },
    //选中
    pitchOn(row) {
      console.log("选中");
      console.log(row);
      console.log(row.index);
      this.form3.contractId=row.id;
      this.toFactoringApply(row.id, this.form1.pricductId, this.form1.interestRate, this.form1.serviceFee, row.index + 1);
    },
    //删除一行的数据
    toDelete(index) {
      this.tableData2;
      this.toSum();
    },
    // 计算总和
    toSum(){
      let rentMoney=0.00;
      let factoringCreditMoney=0.00;
      let serviceMoney=0.00;
      let factoringAdditionalMoney=0.00;
      let monthLimit = 0;
      for (let i = 0; i < this.tableData2.length; i++) {
        console.log("------------")
        console.log(this.tableData2[i]);
        console.log(this.tableData2[i].shouldMonthRent)
        console.log(this.tableData2[i].factoringCreditMoney);
        console.log(this.tableData2[i].serviceFee);
        console.log(this.tableData2[i].factoringInterest);
        rentMoney += this.tableData2[i].shouldMonthRent;
        factoringCreditMoney += this.tableData2[i].factoringCreditMoney;
        serviceMoney += this.tableData2[i].serviceFee;
        factoringAdditionalMoney += this.tableData2[i].factoringInterest;
        if(this.tableData2[i].factoringPeriod>monthLimit){
          monthLimit = this.tableData2[i].factoringPeriod;
        }
      }
      this.form3.rentMoney=rentMoney;
      this.form3.factoringCreditMoney = factoringCreditMoney;
      this.form3.serviceMoney = serviceMoney*1.0;
      this.form3.factoringAdditionalMoney = factoringAdditionalMoney*1.0;
      this.form3.factoringTrueMoney = factoringCreditMoney-serviceMoney-factoringAdditionalMoney;
      this.form3.monthLimit = monthLimit;
    },
    //点击选中后 将参数后台进行处理

    toFactoringApply(id, pricductId, interestRate, serviceFee, row) {
      factoringApply(id, pricductId, interestRate, serviceFee, row).then(res => {
        console.log(res);
        if (res.code==200) {
          alert("1111")
            this.tableData2=res.data;
            this.dialogFormVisible = false;
            this.toSum();
          }
         else {
          this.$message.error("该合同剩余未逾期房租租约金额为0，不可申请租约保理！");
        }
      })
    },
      //提交申请
      goSaveJrFactoring(){
        // 一次性服务费
        this.form3.serviceFee = this.form1.serviceFee;
        // 年利率
        this.form3.rate = this.form1.interestRate;
        //产品id
        this.form3.jrProductId = this.form1.pricductId;
        //保理明细
        this.form3.jrFactoringDetailList = this.tableData2;
        console.log(this.form3);
        saveJrFactoring(this.form3).then(res=>{
          console.log(res)
          if(res.code == 200){
            this.$message.success("处理成功");
            window.location.reload();
          }else{
            this.$message.error("处理失败");
          }
        })
      }
    },
  created() {
    this.getProductTypeList();
  }
};
</script>
